<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>如何快速构建一个简单的程序</title>
    <meta name="description" content="首先我们通过内置的工程模板创建一个空工程:$ xmake create -P ./hellocreate hello ...create ok!👌这个时候xmake将会产生一些工程文件，如下:$ cd ./hello$ tree ..├── src│   └── main.c└── xmake.lua这个简单的程...">

    
    <meta name="keywords" content="xmake,编译,交叉编译,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2016/07/16/how-to-build-a-simple-project/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>如何快速构建一个简单的程序</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2016-07-16
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#xmake" title="Category: xmake" rel="category">xmake</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a-->
        <a href="/cn/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a>&nbsp;
    
        <!--a href="/tag/#%E7%BC%96%E8%AF%91" title="Tag: 编译" rel="tag">编译</a-->
        <a href="/cn/tag/#编译" title="Tag: 编译" rel="tag">编译</a>&nbsp;
    
        <!--a href="/tag/#%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91" title="Tag: 交叉编译" rel="tag">交叉编译</a-->
        <a href="/cn/tag/#交叉编译" title="Tag: 交叉编译" rel="tag">交叉编译</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>首先我们通过内置的工程模板创建一个空工程:</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake create -P ./hello

create hello ...
create ok!👌
</code></pre>
</div>

<p>这个时候xmake将会产生一些工程文件，如下:</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span><span class="nb">cd</span> ./hello
<span class="gp">$ </span>tree .

.
├── src
│   └── main.c
└── xmake.lua
</code></pre>
</div>

<p>这个简单的程序仅仅只是为了打印输出： <code class="highlighter-rouge">hello xmake!</code></p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>cat ./src/main.c 

<span class="c">#include &lt;stdio.h&gt;</span>
int main<span class="o">(</span>int argc, char<span class="k">**</span> argv<span class="o">)</span>
<span class="o">{</span>
    <span class="nb">printf</span><span class="o">(</span><span class="s2">"hello xmake!</span><span class="se">\n</span><span class="s2">"</span><span class="o">)</span>;
    <span class="k">return </span>0;
<span class="o">}</span>
</code></pre>
</div>

<p><code class="highlighter-rouge">xmake.lua</code>是基于lua语法的工程描述文件，它很简单：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="err">$</span> <span class="n">cat</span> <span class="n">xmake</span><span class="p">.</span><span class="n">lua</span> 

<span class="n">target</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span> 

</code></pre>
</div>

<p>现在我们开始编译这个程序</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake

checking <span class="k">for </span>the architecture ... x86_64
checking <span class="k">for </span>the Xcode SDK version <span class="k">for </span>macosx ... 10.11
checking <span class="k">for </span>the target minimal version ... 10.11
checking <span class="k">for </span>the c compiler <span class="o">(</span>cc<span class="o">)</span> ... xcrun -sdk macosx clang
checking <span class="k">for </span>the c++ compiler <span class="o">(</span>cxx<span class="o">)</span> ... xcrun -sdk macosx clang
checking <span class="k">for </span>the objc compiler <span class="o">(</span>mm<span class="o">)</span> ... xcrun -sdk macosx clang
checking <span class="k">for </span>the objc++ compiler <span class="o">(</span>mxx<span class="o">)</span> ... xcrun -sdk macosx clang++
checking <span class="k">for </span>the assember <span class="o">(</span>as<span class="o">)</span> ... xcrun -sdk macosx clang
checking <span class="k">for </span>the linker <span class="o">(</span>ld<span class="o">)</span> ... xcrun -sdk macosx clang++
checking <span class="k">for </span>the static library archiver <span class="o">(</span>ar<span class="o">)</span> ... xcrun -sdk macosx ar
checking <span class="k">for </span>the static library extractor <span class="o">(</span>ex<span class="o">)</span> ... xcrun -sdk macosx ar
checking <span class="k">for </span>the shared library linker <span class="o">(</span>sh<span class="o">)</span> ... xcrun -sdk macosx clang++
checking <span class="k">for </span>the swift compiler <span class="o">(</span>sc<span class="o">)</span> ... xcrun -sdk macosx swiftc
checking <span class="k">for </span>the debugger <span class="o">(</span>dd<span class="o">)</span> ... xcrun -sdk macosx lldb
configure
<span class="o">{</span>
    ex <span class="o">=</span> <span class="s2">"xcrun -sdk macosx ar"</span>
,   ccache <span class="o">=</span> <span class="s2">"ccache"</span>
,   plat <span class="o">=</span> <span class="s2">"macosx"</span>
,   ar <span class="o">=</span> <span class="s2">"xcrun -sdk macosx ar"</span>
,   buildir <span class="o">=</span> <span class="s2">"build"</span>
,   as <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang"</span>
,   sh <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang++"</span>
,   arch <span class="o">=</span> <span class="s2">"x86_64"</span>
,   mxx <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang++"</span>
,   xcode_dir <span class="o">=</span> <span class="s2">"/Applications/Xcode.app"</span>
,   target_minver <span class="o">=</span> <span class="s2">"10.11"</span>
,   sc <span class="o">=</span> <span class="s2">"xcrun -sdk macosx swiftc"</span>
,   mode <span class="o">=</span> <span class="s2">"release"</span>
,   make <span class="o">=</span> <span class="s2">"make"</span>
,   cc <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang"</span>
,   host <span class="o">=</span> <span class="s2">"macosx"</span>
,   dd <span class="o">=</span> <span class="s2">"xcrun -sdk macosx lldb"</span>
,   kind <span class="o">=</span> <span class="s2">"static"</span>
,   ld <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang++"</span>
,   xcode_sdkver <span class="o">=</span> <span class="s2">"10.11"</span>
,   cxx <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang"</span>
,   mm <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang"</span>
<span class="o">}</span>
configure ok!
clean ok!
<span class="o">[</span>00%]: ccache compiling.release src/main.c
<span class="o">[</span>100%]: linking.release hello
build ok!👌
</code></pre>
</div>

<p>接着运行它:</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake run hello

hello world!
</code></pre>
</div>

<p>或者进行调试</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake run -d hello 

<span class="o">[</span>lldb]<span class="nv">$target</span> create <span class="s2">"build/hello"</span>
Current executable <span class="nb">set </span>to <span class="s1">'build/hello'</span> <span class="o">(</span>x86_64<span class="o">)</span>.
<span class="o">[</span>lldb]<span class="nv">$b</span> main
Breakpoint 1: where <span class="o">=</span> hello<span class="sb">`</span>main, address <span class="o">=</span> 0x0000000100000f50
<span class="o">[</span>lldb]<span class="nv">$r</span>
Process 7509 launched: <span class="s1">'/private/tmp/hello/build/hello'</span> <span class="o">(</span>x86_64<span class="o">)</span>
Process 7509 stopped
<span class="k">*</span> thread <span class="c">#1: tid = 0x435a2, 0x0000000100000f50 hello`main, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1</span>
    frame <span class="c">#0: 0x0000000100000f50 hello`main</span>
hello<span class="sb">`</span>main:
<span class="gp">-&gt;  </span>0x100000f50 &lt;+0&gt;:  pushq  %rbp
    0x100000f51 &lt;+1&gt;:  movq   %rsp, %rbp
    0x100000f54 &lt;+4&gt;:  leaq   0x2b<span class="o">(</span>%rip<span class="o">)</span>, %rdi          ; <span class="s2">"hello world!"</span>
    0x100000f5b &lt;+11&gt;: callq  0x100000f64               ; symbol stub <span class="k">for</span>: puts
<span class="o">[</span>lldb]<span class="err">$</span>
</code></pre>
</div>

<p>接着我们尝试构建一个android版本，这个时候得设置ndk路径，当然也能配置到全局配置中，一劳永逸</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -p android --ndk<span class="o">=</span>~/files/android-ndk-r10e/

checking <span class="k">for </span>the architecture ... armv7-a
checking <span class="k">for </span>the SDK version of NDK ... android-21
checking <span class="k">for </span>the c compiler <span class="o">(</span>cc<span class="o">)</span> ... arm-linux-androideabi-gcc
checking <span class="k">for </span>the c++ compiler <span class="o">(</span>cxx<span class="o">)</span> ... arm-linux-androideabi-g++
checking <span class="k">for </span>the assember <span class="o">(</span>as<span class="o">)</span> ... arm-linux-androideabi-gcc
checking <span class="k">for </span>the linker <span class="o">(</span>ld<span class="o">)</span> ... arm-linux-androideabi-g++
checking <span class="k">for </span>the static library archiver <span class="o">(</span>ar<span class="o">)</span> ... arm-linux-androideabi-ar
checking <span class="k">for </span>the static library extractor <span class="o">(</span>ex<span class="o">)</span> ... arm-linux-androideabi-ar
checking <span class="k">for </span>the shared library linker <span class="o">(</span>sh<span class="o">)</span> ... arm-linux-androideabi-g++
configure
<span class="o">{</span>
    ex <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ar"</span>
,   ccache <span class="o">=</span> <span class="s2">"ccache"</span>
,   ndk <span class="o">=</span> <span class="s2">"~/files/android-ndk-r10e/"</span>
,   sc <span class="o">=</span> <span class="s2">"xcrun -sdk macosx swiftc"</span>
,   ar <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ar"</span>
,   ld <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++"</span>
,   buildir <span class="o">=</span> <span class="s2">"build"</span>
,   host <span class="o">=</span> <span class="s2">"macosx"</span>
,   as <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc"</span>
,   toolchains <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin"</span>
,   arch <span class="o">=</span> <span class="s2">"armv7-a"</span>
,   mxx <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang++"</span>
,   xcode_dir <span class="o">=</span> <span class="s2">"/Applications/Xcode.app"</span>
,   target_minver <span class="o">=</span> <span class="s2">"10.11"</span>
,   ndk_sdkver <span class="o">=</span> 21
,   mode <span class="o">=</span> <span class="s2">"release"</span>
,   cc <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc"</span>
,   cxx <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++"</span>
,   make <span class="o">=</span> <span class="s2">"make"</span>
,   dd <span class="o">=</span> <span class="s2">"xcrun -sdk macosx lldb"</span>
,   kind <span class="o">=</span> <span class="s2">"static"</span>
,   sh <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++"</span>
,   xcode_sdkver <span class="o">=</span> <span class="s2">"10.11"</span>
,   plat <span class="o">=</span> <span class="s2">"android"</span>
,   mm <span class="o">=</span> <span class="s2">"xcrun -sdk macosx clang"</span>
<span class="o">}</span>
configure ok!

<span class="gp">$ </span>xmake

clean ok!
<span class="o">[</span>00%]: ccache compiling.release src/main.c
<span class="o">[</span>100%]: linking.release hello
build ok!👌
</code></pre>
</div>

<p>或者我们编一个iphoneos的版本，例如：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -p iphoneos

checking <span class="k">for </span>the architecture ... armv7
checking <span class="k">for </span>the Xcode SDK version <span class="k">for </span>iphoneos ... 9.2
checking <span class="k">for </span>the target minimal version ... 9.2
checking <span class="k">for </span>the c compiler <span class="o">(</span>cc<span class="o">)</span> ... xcrun -sdk iphoneos clang
checking <span class="k">for </span>the c++ compiler <span class="o">(</span>cxx<span class="o">)</span> ... xcrun -sdk iphoneos clang
checking <span class="k">for </span>the objc compiler <span class="o">(</span>mm<span class="o">)</span> ... xcrun -sdk iphoneos clang
checking <span class="k">for </span>the objc++ compiler <span class="o">(</span>mxx<span class="o">)</span> ... xcrun -sdk iphoneos clang++
checking <span class="k">for </span>the assember <span class="o">(</span>as<span class="o">)</span> ... gas-preprocessor.pl xcrun -sdk iphoneos clang
checking <span class="k">for </span>the linker <span class="o">(</span>ld<span class="o">)</span> ... xcrun -sdk iphoneos clang++
checking <span class="k">for </span>the static library archiver <span class="o">(</span>ar<span class="o">)</span> ... xcrun -sdk iphoneos ar
checking <span class="k">for </span>the static library extractor <span class="o">(</span>ex<span class="o">)</span> ... xcrun -sdk iphoneos ar
checking <span class="k">for </span>the shared library linker <span class="o">(</span>sh<span class="o">)</span> ... xcrun -sdk iphoneos clang++
checking <span class="k">for </span>the swift compiler <span class="o">(</span>sc<span class="o">)</span> ... xcrun -sdk iphoneos swiftc
configure
<span class="o">{</span>
    ex <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos ar"</span>
,   ccache <span class="o">=</span> <span class="s2">"ccache"</span>
,   ndk <span class="o">=</span> <span class="s2">"~/files/android-ndk-r10e/"</span>
,   sc <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos swiftc"</span>
,   ar <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos ar"</span>
,   sh <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos clang++"</span>
,   buildir <span class="o">=</span> <span class="s2">"build"</span>
,   xcode_dir <span class="o">=</span> <span class="s2">"/Applications/Xcode.app"</span>
,   as <span class="o">=</span> <span class="s2">"/usr/local/share/xmake/tools/utils/gas-preprocessor.pl xcrun -sdk iphoneos clang"</span>
,   toolchains <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin"</span>
,   arch <span class="o">=</span> <span class="s2">"armv7"</span>
,   mxx <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos clang++"</span>
,   ndk_sdkver <span class="o">=</span> 21
,   target_minver <span class="o">=</span> <span class="s2">"9.2"</span>
,   cc <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos clang"</span>
,   mode <span class="o">=</span> <span class="s2">"release"</span>
,   host <span class="o">=</span> <span class="s2">"macosx"</span>
,   cxx <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos clang"</span>
,   make <span class="o">=</span> <span class="s2">"make"</span>
,   dd <span class="o">=</span> <span class="s2">"xcrun -sdk macosx lldb"</span>
,   kind <span class="o">=</span> <span class="s2">"static"</span>
,   ld <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos clang++"</span>
,   xcode_sdkver <span class="o">=</span> <span class="s2">"9.2"</span>
,   plat <span class="o">=</span> <span class="s2">"iphoneos"</span>
,   mm <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos clang"</span>
<span class="o">}</span>
configure ok!

<span class="gp">$ </span>xmake
 
<span class="o">[</span>00%]: ccache compiling.release src/main.c
<span class="o">[</span>100%]: linking.release hello
build ok!👌
</code></pre>
</div>

<p>最后我们尝试为mingw平台进行编译，sdk指定交叉工具链目录，交叉编译linux平台也可以这么用哦。。</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -p mingw --sdk<span class="o">=</span>/usr/local/i386-mingw32-4.3.0/

checking <span class="k">for </span>the architecture ... i386
checking <span class="k">for </span>the c compiler <span class="o">(</span>cc<span class="o">)</span> ... i386-mingw32-gcc
checking <span class="k">for </span>the c++ compiler <span class="o">(</span>cxx<span class="o">)</span> ... i386-mingw32-g++
checking <span class="k">for </span>the assember <span class="o">(</span>as<span class="o">)</span> ... i386-mingw32-gcc
checking <span class="k">for </span>the linker <span class="o">(</span>ld<span class="o">)</span> ... i386-mingw32-g++
checking <span class="k">for </span>the static library archiver <span class="o">(</span>ar<span class="o">)</span> ... i386-mingw32-ar
checking <span class="k">for </span>the static library extractor <span class="o">(</span>ex<span class="o">)</span> ... i386-mingw32-ar
checking <span class="k">for </span>the shared library linker <span class="o">(</span>sh<span class="o">)</span> ... i386-mingw32-g++
checking <span class="k">for </span>the swift compiler <span class="o">(</span>sc<span class="o">)</span> ... no
configure
<span class="o">{</span>
    ex <span class="o">=</span> <span class="s2">"/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-ar"</span>
,   ccache <span class="o">=</span> <span class="s2">"ccache"</span>
,   ndk <span class="o">=</span> <span class="s2">"~/files/android-ndk-r10e/"</span>
,   sc <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos swiftc"</span>
,   sdk <span class="o">=</span> <span class="s2">"/usr/local/i386-mingw32-4.3.0/"</span>
,   cc <span class="o">=</span> <span class="s2">"/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-gcc"</span>
,   ndk_sdkver <span class="o">=</span> 21
,   buildir <span class="o">=</span> <span class="s2">"build"</span>
,   plat <span class="o">=</span> <span class="s2">"mingw"</span>
,   as <span class="o">=</span> <span class="s2">"/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-gcc"</span>
,   toolchains <span class="o">=</span> <span class="s2">"/Users/ruki/files/android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin"</span>
,   arch <span class="o">=</span> <span class="s2">"i386"</span>
,   mxx <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos clang++"</span>
,   xcode_dir <span class="o">=</span> <span class="s2">"/Applications/Xcode.app"</span>
,   target_minver <span class="o">=</span> <span class="s2">"9.2"</span>
,   sh <span class="o">=</span> <span class="s2">"/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-g++"</span>
,   mode <span class="o">=</span> <span class="s2">"release"</span>
,   host <span class="o">=</span> <span class="s2">"macosx"</span>
,   cxx <span class="o">=</span> <span class="s2">"/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-g++"</span>
,   make <span class="o">=</span> <span class="s2">"make"</span>
,   dd <span class="o">=</span> <span class="s2">"xcrun -sdk macosx lldb"</span>
,   kind <span class="o">=</span> <span class="s2">"static"</span>
,   ar <span class="o">=</span> <span class="s2">"/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-ar"</span>
,   xcode_sdkver <span class="o">=</span> <span class="s2">"9.2"</span>
,   ld <span class="o">=</span> <span class="s2">"/usr/local/i386-mingw32-4.3.0/bin/i386-mingw32-g++"</span>
,   mm <span class="o">=</span> <span class="s2">"xcrun -sdk iphoneos clang"</span>
<span class="o">}</span>
configure ok!

<span class="gp">$ </span>xmake

<span class="o">[</span>00%]: ccache compiling.release src/main.c
<span class="o">[</span>100%]: linking.release hello.exe
build ok!👌
</code></pre>
</div>

<p>xmake还能直接在windows的cmd终端下，进行直接编译windows的程序，它会去自动检测当前系统装的vs环境，调用里面的cl.exe编译器进行编译，一切都是自动化的，我们不需要额外配置什么，只需要执行：<code class="highlighter-rouge">xmake</code> 就行了。。</p>

<p>例如：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake

checking <span class="k">for </span>the architecture ... x86
checking <span class="k">for </span>the Microsoft Visual Studio version ... 2008
checking <span class="k">for </span>the c compiler <span class="o">(</span>cc<span class="o">)</span> ... cl.exe
checking <span class="k">for </span>the c++ compiler <span class="o">(</span>cxx<span class="o">)</span> ... cl.exe
checking <span class="k">for </span>the assember <span class="o">(</span>as<span class="o">)</span> ... ml.exe
checking <span class="k">for </span>the linker <span class="o">(</span>ld<span class="o">)</span> ... link.exe
checking <span class="k">for </span>the static library archiver <span class="o">(</span>ar<span class="o">)</span> ... link.exe -lib
checking <span class="k">for </span>the shared library linker <span class="o">(</span>sh<span class="o">)</span> ... link.exe -dll
checking <span class="k">for </span>the static library extractor <span class="o">(</span>ex<span class="o">)</span> ... lib.exe
configure
<span class="o">{</span>
    ex <span class="o">=</span> <span class="s2">"lib.exe"</span>
,   sh <span class="o">=</span> <span class="s2">"link.exe -dll"</span>
,   host <span class="o">=</span> <span class="s2">"windows"</span>
,   ar <span class="o">=</span> <span class="s2">"link.exe -lib"</span>
,   as <span class="o">=</span> <span class="s2">"ml.exe"</span>
,   plat <span class="o">=</span> <span class="s2">"windows"</span>
,   buildir <span class="o">=</span> <span class="s2">"build"</span>
,   arch <span class="o">=</span> <span class="s2">"x86"</span>
,   cc <span class="o">=</span> <span class="s2">"cl.exe"</span>
,   cxx <span class="o">=</span> <span class="s2">"cl.exe"</span>
,   mode <span class="o">=</span> <span class="s2">"release"</span>
,   clean <span class="o">=</span> <span class="nb">true</span>
,   kind <span class="o">=</span> <span class="s2">"static"</span>
,   ld <span class="o">=</span> <span class="s2">"link.exe"</span>
,   vs <span class="o">=</span> <span class="s2">"2008"</span>
<span class="o">}</span>
configure ok!
<span class="o">[</span>00%]: compiling.release src<span class="se">\m</span>ain.c
<span class="o">[</span>100%]: linking.release hello.exe
build ok!
</code></pre>
</div>

<p>顺便说一下，在windows下编译，xmake是完全支持多任务的哦，默认就是自动多任务构建的，比起以前在msys, cygwin里面用 gmake来编译快多了，因为windows下的gmake就算你启用了<code class="highlighter-rouge">-j 4</code> 也没啥效果，非常非常得慢。。。</p>


        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
                    
                    <h2 id="english">English</h2>
                    <ul>
                    
                    <li class="relatedPost">
                        <a href="/2016/07/16/how-to-build-a-simple-project/">How to build a simple project quickly
                        
                        </a>
                    </li>
                    
                    
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        
            </ul>
        

        
        
            
        
            
            
            
                
                    
                        
                        <h2 id="similar_posts">相关文章</h2>
                        <ul>
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/17/xmake-update-v2.6.2/">xmake v2.6.2 发布，新增 Linux 内核驱动模块构建支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/03/xmake-update-v2.6.1/">xmake v2.6.1 发布，使用 Lua5.4 运行时，Rust 和 C++ 混合编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/30/xmake-update-v2.5.9/">xmake v2.5.9 发布，改进 C++20 模块，并支持 Nim, Keil MDK 和 Unity Build
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/08/xmake-update-v2.5.8/">xmake v2.5.8 发布，新增 Pascal/Swig 程序和 Lua53 运行时支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/08/29/xmake-update-v2.5.7/">xmake v2.5.7 发布，包依赖锁定和 Vala/Metal 语言编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
        
            </ul>
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2016/07/14/plugin-print-colors/">插件开发之色彩高亮显示</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2016/07/16/run-debug/">利用xmake运行和调试程序</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
